﻿namespace WMS6.Web.TransportManagement
{
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.TransportMgtServiceRef;

    public class Bol : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox cbxPaid;
        protected DropDownList ddlStatus;
        protected HtmlForm form1;
        protected HtmlInputHidden hdnBolID;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hiddenFlag;
        protected Label lblBol;
        protected Label lblBolID;
        protected Label lblCreatedBy;
        protected Label lblCreatedDate;
        protected Label lblCube;
        protected Label lblDepartDate;
        protected Label lblEquipmentID;
        protected Label lblFreightDealer;
        protected Label lblFreightSelf;
        protected Label lblGrossWeight;
        protected Label lblMsg;
        protected Label lblNetWeight;
        protected Label lblQty;
        protected Label lblRemark;
        protected Label lblStatus;
        protected Label lblUdf04;
        protected Label lblUdf09;
        protected Label lblUdf1;
        protected Label lblUdf10;
        protected Label lblUdf2;
        protected Label lblUdf3;
        protected Label lblUdf5;
        protected Label lblUdf6;
        protected Label lblUdf7;
        protected Label lblUdf8;
        protected LinkButton lnkbtnCancelPay;
        protected LinkButton lnkbtnPay;
        protected LinkButton lnkbtnSaveFreight;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlnkbtnCancelPay;
        protected HtmlTableCell tdlnkbtnPay;
        protected HtmlTableCell tdlnkbtnSaveFreight;
        protected TextBox txtBolID;
        protected TextBox txtCreatedBy;
        protected TextBox txtCreatedDate;
        protected TextBox txtCube;
        protected TextBox txtDepartDate;
        protected TextBox txtEquipmentID;
        protected TextBox txtFreightDealer;
        protected TextBox txtFreightSelf;
        protected TextBox txtGrossWeight;
        protected TextBox txtNetWeight;
        protected TextBox txtQty;
        protected TextBox txtRemark;
        protected TextBox txtUdf1;
        protected TextBox txtUdf10;
        protected TextBox txtUdf2;
        protected TextBox txtUdf3;
        protected TextBox txtUdf4;
        protected TextBox txtUdf5;
        protected TextBox txtUdf6;
        protected TextBox txtUdf7;
        protected TextBox txtUdf8;
        protected TextBox txtUdf9;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "list":
                    base.Response.Redirect("BolList.aspx?from=edit", false);
                    break;

                case "trackinglist":
                    base.Response.Redirect("TrackingList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.UpdateSkuInfo();
        }

        private BolInfo GetSkuInfoFromForm()
        {
            BolInfo bolInfo = base.DataEntityViewState as BolInfo;
            bolInfo.GrossWeight = !string.IsNullOrEmpty(this.txtGrossWeight.Text.Trim()) ? decimal.Parse(this.txtGrossWeight.Text.Trim()) : 0M;
            bolInfo.NetWeight = !string.IsNullOrEmpty(this.txtNetWeight.Text.Trim()) ? decimal.Parse(this.txtNetWeight.Text.Trim()) : 0M;
            bolInfo.Cube = !string.IsNullOrEmpty(this.txtCube.Text.Trim()) ? decimal.Parse(this.txtCube.Text.Trim()) : 0M;
            bolInfo.Udf1 = this.txtUdf1.Text.Trim();
            bolInfo.Udf2 = this.txtUdf2.Text.Trim();
            bolInfo.Udf3 = this.txtUdf3.Text.Trim();
            bolInfo.Udf4 = this.txtUdf4.Text.Trim();
            bolInfo.Udf5 = this.txtUdf5.Text.Trim();
            bolInfo.Udf6 = this.txtUdf6.Text.Trim();
            bolInfo.Udf7 = this.txtUdf7.Text.Trim();
            bolInfo.Udf8 = this.txtUdf8.Text.Trim();
            bolInfo.Udf9 = this.txtUdf9.Text.Trim();
            bolInfo.Udf10 = this.txtUdf10.Text.Trim();
            return bolInfo;
        }

        protected void lnkbtnCancelPay_Click(object sender, EventArgs e)
        {
            try
            {
                if (!this.cbxPaid.Checked)
                {
                    this.lblMsg.Text = "装车单未付款,无法取消.";
                }
                else
                {
                    string bolID = this.hdnBolID.Value;
                    BolInfo bolInfo = TransportMgtServiceClientProxy.GetInstance().GetBolInfoByID(bolID);
                    bolInfo.Paid = "N";
                    bolInfo.UpdatedBy = base.UserInfo.UserID;
                    bolInfo.UpdatedDate = DateTime.Now;
                    this.lblMsg.Text = TransportMgtServiceClientProxy.GetInstance().UpdateBolInfo(bolInfo) + "取消付款操作执行成功.";
                    this.SetBolInfo2Form();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnPay_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.cbxPaid.Checked)
                {
                    this.lblMsg.Text = "装车单已付款,不能重复操作.";
                }
                else
                {
                    string bolID = this.hdnBolID.Value;
                    BolInfo bolInfo = TransportMgtServiceClientProxy.GetInstance().GetBolInfoByID(bolID);
                    bolInfo.Paid = "Y";
                    bolInfo.UpdatedBy = base.UserInfo.UserID;
                    bolInfo.UpdatedDate = DateTime.Now;
                    this.lblMsg.Text = TransportMgtServiceClientProxy.GetInstance().UpdateBolInfo(bolInfo) + "付款操作执行成功.";
                    this.SetBolInfo2Form();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void lnkbtnSaveFreight_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.cbxPaid.Checked)
                {
                    this.lblMsg.Text = "装车单已付款.";
                }
                else
                {
                    string bolID = this.hdnBolID.Value;
                    BolInfo bolInfo = TransportMgtServiceClientProxy.GetInstance().GetBolInfoByID(bolID);
                    bolInfo.FreightSelf = !string.IsNullOrEmpty(this.txtFreightSelf.Text.Trim()) ? decimal.Parse(this.txtFreightSelf.Text.Trim()) : 0M;
                    bolInfo.FreightDealer = !string.IsNullOrEmpty(this.txtFreightDealer.Text.Trim()) ? decimal.Parse(this.txtFreightDealer.Text.Trim()) : 0M;
                    bolInfo.UpdatedBy = base.UserInfo.UserID;
                    bolInfo.UpdatedDate = DateTime.Now;
                    this.lblMsg.Text = TransportMgtServiceClientProxy.GetInstance().UpdateBolInfo(bolInfo) + "运费更新成功";
                    this.SetBolInfo2Form();
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlStatus, "BOLStatus");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnFlag.Value = "1";
                this.hdnBolID.Value = base.Request.QueryString["ID"];
                this.txtBolID.Text = base.Request.QueryString["ID"];
                this.SetBolInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["currTab"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one'," + base.Request.QueryString["currTab"].ToString() + ",2);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.btnSave.Enabled = true;
                this.btnGoBack.Enabled = true;
                this.LoadData();
            }
        }

        private void SetBolInfo2Form()
        {
            try
            {
                string bolID = this.hdnBolID.Value;
                BolInfo bolInfo = TransportMgtServiceClientProxy.GetInstance().GetBolInfoByID(bolID);
                base.DataEntityViewState = bolInfo;
                if (bolInfo != null)
                {
                    this.SetBolInfo2Form(bolInfo);
                    this.txtBolID.Enabled = false;
                    this.txtEquipmentID.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void SetBolInfo2Form(BolInfo bolInfo)
        {
            this.txtBolID.Text = bolInfo.BolID;
            this.txtEquipmentID.Text = bolInfo.TransportEquipmentID;
            this.ddlStatus.Text = bolInfo.Status;
            this.txtDepartDate.Text = base.GetPreferenceShortDate(bolInfo.DepartDate);
            this.txtQty.Text = base.GetPreferenceDigit(bolInfo.Qty);
            this.txtGrossWeight.Text = base.GetPreferenceDigit(bolInfo.GrossWeight);
            this.txtNetWeight.Text = base.GetPreferenceDigit(bolInfo.NetWeight);
            this.txtCube.Text = base.GetPreferenceDigit(bolInfo.Cube);
            this.txtBolID.Enabled = false;
            this.txtEquipmentID.Enabled = false;
            this.ddlStatus.Enabled = false;
            this.txtDepartDate.Enabled = false;
            this.txtQty.Enabled = false;
            this.txtGrossWeight.Enabled = true;
            this.txtNetWeight.Enabled = true;
            this.txtCube.Enabled = true;
            this.txtFreightSelf.Text = base.GetPreferenceDigit(bolInfo.FreightSelf);
            this.txtFreightDealer.Text = base.GetPreferenceDigit(bolInfo.FreightDealer);
            this.cbxPaid.Checked = bolInfo.Paid == "Y";
            this.txtFreightDealer.Enabled = bolInfo.Paid == "N";
            this.txtFreightSelf.Enabled = bolInfo.Paid == "N";
            this.cbxPaid.Enabled = false;
            this.txtCreatedBy.Text = bolInfo.CreatedBy;
            this.txtCreatedDate.Text = base.GetPreferenceShortDate(bolInfo.CreatedDate);
            this.txtRemark.Text = bolInfo.Remark;
            this.txtUdf1.Text = bolInfo.Udf1;
            this.txtUdf2.Text = bolInfo.Udf2;
            this.txtUdf3.Text = bolInfo.Udf3;
            this.txtUdf4.Text = bolInfo.Udf4;
            this.txtUdf5.Text = bolInfo.Udf5;
            this.txtUdf6.Text = bolInfo.Udf6;
            this.txtUdf7.Text = bolInfo.Udf7;
            this.txtUdf8.Text = bolInfo.Udf8;
            this.txtUdf9.Text = bolInfo.Udf9;
            this.txtUdf10.Text = bolInfo.Udf10;
        }

        private void UpdateSkuInfo()
        {
            try
            {
                BolInfo bolInfo = this.GetSkuInfoFromForm();
                bolInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = TransportMgtServiceClientProxy.GetInstance().UpdateBolInfo(bolInfo) + base.GetGlobalMsgResource("C002");
                this.SetBolInfo2Form();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

